package com.google.coffeeshop.gui.desktop.product;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.DefaultTableModel;

import com.google.coffeeshop.entity.OrderLineDTO;
import com.google.coffeeshop.entity.ProductGroupDTO;
import com.google.coffeeshop.gui.desktop.AbstractBaseTable;

public class ProductGroupTable  extends AbstractBaseTable {


	private static final long serialVersionUID = 1L;
	
	private static String COL_OL_ID = "Product Group ID";
	private static String COL_NAME = "Group Name";
	private static String COL_CODE = "Code";
	private static String COL_DESCRIPTION = "Description";
	
	private static Object[][] rowData = new Object[][] {};
	private static String[] columnNames = new String[] {
		COL_OL_ID,COL_NAME ,COL_CODE, COL_DESCRIPTION
		};
	
	private DefaultTableModel model = new DefaultTableModel(rowData, columnNames);
	
	public ProductGroupTable() {
		super(rowData, columnNames);
		setModel(model);
		invisibleColumn(0);
		
	}
	
	public void removeAllRow() {
		model.setRowCount(0);
	}
	public void resetTableModel() {
		
	}
	
	private void invisibleColumn(int columnIndex) {
		this.getColumnModel().removeColumn(this.getColumnModel().getColumn(columnIndex));
	}
	public void addNewRows(List<ProductGroupDTO> groups) {
		for (ProductGroupDTO group : groups) {
			addNewRow(group);
		}
		arrageOrderLineNumbers();
	}
	
	private void addNewRow(long groupId,String name, String code, String des ) {
		Object[] objects = new Object[] {groupId, name,code,des };
		model.addRow(objects);
	}

	private void addNewRow(ProductGroupDTO productGroupDTO) {
		addNewRow(productGroupDTO.getId(), productGroupDTO.getName(), productGroupDTO.getCode(), productGroupDTO.getDescription());
	}
	
	
	
	public long getSelectedProductId() {
		int rowIndex = this.getSelectedRow();
		return (long)findModelValue(model, COL_OL_ID, rowIndex);
	}
	
	public List<Long> getSelectedProductIds() {
		List<Long> ids = new ArrayList<Long>();
		for (int rowIndex : this.getSelectedRows()) {
			ids.add((long)findModelValue(model, COL_OL_ID, rowIndex));
		}
		return ids;
	}
	
	

	private void arrageOrderLineNumbers() {
		for (int rowIndex = 0; rowIndex < model.getRowCount(); rowIndex++) {
			model.setValueAt(rowIndex + 1, rowIndex, 1);
		}
	}
}
